package org.springframework.webflow.samples.booking;

import java.security.Principal;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HotelsController {
	
	private static final Logger logging = LoggerFactory.getLogger(HotelsController.class);
    private BookingService bookingService;

    @Autowired
    public HotelsController(BookingService bookingService) {
    	logging.info("HotelsController ---> BookingService bookingService");
    	this.bookingService = bookingService;
    }

    
    @RequestMapping(value = "/hotels/search", method = RequestMethod.GET)
    public void search(SearchCriteria searchCriteria, Principal currentUser, Model model) {
    	logging.info("Welcome search--->/hotels/search currentUser?" + ((currentUser==null) ? "currentUser is null" : "currentUser is not null"));
    	
		if (currentUser != null) {		
		    List<Booking> booking = bookingService.findBookings(currentUser.getName());
		    model.addAttribute(booking);
		}
    }

    @RequestMapping(value = "/hotels", method = RequestMethod.GET)
    public String list(SearchCriteria criteria, Model model) {
    	logging.info("Welcome list--->/hotels");
    	List<Hotel> hotels = bookingService.findHotels(criteria);
    	model.addAttribute(hotels);
    	return "hotels/list";
    }

    @RequestMapping(value = "/hotels/{id}", method = RequestMethod.GET)
    public String show(@PathVariable Long id, Model model) {
    	logging.info("Welcome show--->/hotels/{id}="+id);
    	model.addAttribute(bookingService.findHotelById(id));
    	return "hotels/show";
    }

    @RequestMapping(value = "/bookings/{id}", method = RequestMethod.DELETE)
    public String deleteBooking(@PathVariable Long id) {
    	logging.info("Welcome deleteBooking--->/bookings/{id}="+id);
    	bookingService.cancelBooking(id);
    	return "redirect:../hotels/search";
    }
    
    
}
